let loginForm = document.getElementsByName('loginForm')[0];
let adminName = document.getElementsByName('username')[0];
let passWord = document.getElementsByName('pwd')[0];

import { saveToken,saveUser } from "../../utils/storageUtils";
import axios from 'axios';

// console.log(window.location);

// 提交事件
loginForm.onsubmit =async function(e){

    // 清除浏览器默认行为
    e.preventDefault();
    // console.log("提交了");
    let username = adminName.value;
    let pwd = passWord.value;
    //http://localhost:8082/admin/login
    let {data} = await axios({
        url:"http://localhost:8082/admin/login",
        method:"POST",
        // data:`username=${username}&pwd=${pwd}`
        data:{
            username,
            pwd
        }
    });

    if(data.code === 2000){
        // 登录成功

        // 保存token
        saveToken(data.data.token)
        // 保存用户信息
        saveUser(data.data.user)

        window.location.href = 'http://localhost:3000/users.html';
        
    }else if(data.code === 2002){
        passWord.value = '';
        alert('账号或密码错误')
    }
    else{
        alert(data.message)
    }
}